package floatWindow2;

import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;

import com.example.livesettingtest.MyConstant;
import com.example.livesettingtest.R;


/**
 * 悬浮窗在服务中创建，通过暴露接口FloatCallBack与Activity进行交互
 */
public class FloatMonkService extends Service implements FloatCallBack {
    String TAG = "FloatMonkService";
    /**
     * home键监听
     */
    private HomeWatcherReceiver mHomeKeyReceiver;

    @Override
    public void onCreate() {
        super.onCreate();
        FloatActionController.getInstance().registerCallLittleMonk(this);
        //注册广播接收者
        mHomeKeyReceiver = new HomeWatcherReceiver();
        final IntentFilter homeFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
        registerReceiver(mHomeKeyReceiver, homeFilter);

    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    /**
     * 初始化WindowManager
     */
    private void initWindowData(String tipText) {
        FloatWindowManager.createFloatWindow(this,tipText);
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        MyConstant.tipType floatType = (MyConstant.tipType) intent.getSerializableExtra("floatWindowType");

        switch (floatType)
        {
            case AUTO_START:
                //初始化悬浮窗UI
                String auto_start = this.getResources().getString(R.string.auto_start);
                initWindowData(auto_start);
                break;
            case AUTO_START7:
                String auto_start7 = this.getResources().getString(R.string.auto_start_7);
                initWindowData(auto_start7);
                break;
            case LOCK_SCREEN:
                String lock_screen = this.getResources().getString(R.string.lock_screen);
                initWindowData(lock_screen);
                break;
            case MOBILE_MANAGER:
                String mobile_manager = this.getResources().getString(R.string.mobile_manager);
                initWindowData(mobile_manager);
                break;
            case NOTIFY_SETTING:
                String notify_setting = this.getResources().getString(R.string.notify_setting);
                initWindowData(notify_setting);
                break;
            case PERMISSION_NOTIFY:
                String permission_notify = this.getResources().getString(R.string.permission_notify);
                initWindowData(permission_notify);
                break;
            case IGNORE_BATTERY:
                String ignore_battery = this.getResources().getString(R.string.ignore_battery);
                initWindowData(ignore_battery);
                break;
            case PERMISSION:
                String permission = this.getResources().getString(R.string.permission);
                initWindowData(permission);
                break;
            case WLAN_ALWAYS:
                String wlan_always = this.getResources().getString(R.string.wlan_always);
                initWindowData(wlan_always);
                break;

            case BATTERY_WLAN:
                String battery_walan = this.getResources().getString(R.string.BATTERY);
                initWindowData(battery_walan);
                break;
            case VIVO_POWER_CONSUME:
                String powerCounsume = this.getResources().getString(R.string.power_consume);
                initWindowData(powerCounsume);
                break;
            case VIVO_PERMISSION:
                String vivo_permission = this.getResources().getString(R.string.vivo_permission);
                initWindowData(vivo_permission);
                break;
            case VIVO_WLAN_ALWAYS:
                String vivo_wlan_always = this.getResources().getString(R.string.vivo_wlan_always);
                initWindowData(vivo_wlan_always);
                break;
            case VIVO_IGNORE_BATTERY:
                String vivo_ignore_battery = this.getResources().getString(R.string.vivo_ignore_battery);
                initWindowData(vivo_ignore_battery);
                break;
            case VIVO_PERMISSION2:
                String vivo_permission2 = this.getResources().getString(R.string.vivo_permission2);
                initWindowData(vivo_permission2);
                break;

            case VIVO_NOTIFY:
                String VIVO_NOTIFY = this.getResources().getString(R.string.vivo_notify0);
                initWindowData(VIVO_NOTIFY);
                break;
            case VIVO_NOTIFY6:
                String VIVO_NOTIFY6 = this.getResources().getString(R.string.vivo_notify6);
                initWindowData(VIVO_NOTIFY6);
                break;
            case SETTINGS:
                initWindowData("请从设置页面开始设置");
                break;
            case VIVO_SETTINGS_PERMISSION:   //VIVO权限设置
                String vivo_settings_permission = this.getResources().getString(R.string.vivo_settings_permission);
                initWindowData(vivo_settings_permission);
                break;
            case OPPO_OPEN_PERMISSION:  //OPPO打开权限
                String oppo_open_permission = this.getResources().getString(R.string.oppo_open_permission);
                initWindowData(oppo_open_permission);
                break;
            case OPPO_AUTO_ASSIGN:  //自启动，关联启动
                String oppo_auto_assign = this.getResources().getString(R.string.oppo_auto_assign);
                initWindowData(oppo_auto_assign);
                break;
            case OPPO_BATTERY_PROTECT:
                String oppo_battery_protected_tip = this.getResources().getString(R.string.oppo_battery_protected_tip);
                initWindowData(oppo_battery_protected_tip);
                break;
            case OPPO_BATTERY_PROTECT_HIGH_OS:
                String oppo_battery_protected_high_os_tip = this.getResources().getString(R.string.oppo_battery_protected_high_os_tip);
                initWindowData(oppo_battery_protected_high_os_tip);
                break;
            case OPPO_PERMISSION_NOTIFY:
                String oppo_permission_notify = this.getResources().getString(R.string.oppo_permission_notify);
                initWindowData(oppo_permission_notify);
                break;

        }



        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //移除悬浮窗
        FloatWindowManager.removeFloatWindowManager();
        FloatWindowManager.removeSmallWindow();//gxw+
        Log.i(TAG,"Service onDestroy()");

        //注销广播接收者
        if (null != mHomeKeyReceiver) {
            unregisterReceiver(mHomeKeyReceiver);
        }
    }

    /////////////////////////////////////////////////////////实现接口////////////////////////////////////////////////////
/*gxw-s    @Override
    public void guideUser(int type) {
        FloatWindowManager.updataRedAndDialog(this);
    }gxw-e*/


    /**
     * 悬浮窗的隐藏
     */
    @Override
    public void hide() {
        FloatWindowManager.hide();
    }

    @Override
    public void showSmallWindow() {
        FloatWindowManager.showSmallWindow();//gxw+
    }

    @Override
    public void hideSmallWindow() {
        FloatWindowManager.hideSmallWindow();//gxw+
    }

    //gxw+s
  @Override
    public void addObtainNumer() {

    }

    @Override
    public void setObtainNumber(int number) {

    }

    @Override
    public void guideUser(int type) {

    }
    //gxw+e

    /**
     * 悬浮窗的显示
     */
    @Override
    public void show() {
        FloatWindowManager.show();
    }

    /**
     * 添加数量
     */
/*gxw-s    @Override
    public void addObtainNumer() {
        FloatWindowManager.addObtainNumer(this);
        guideUser(4);
    }



     * 减少数量

    @Override
    public void setObtainNumber(int number) {
        FloatWindowManager.setObtainNumber(this, number);
    }
    gxw-e*/


}
